package ru.tatintec.gwttest.server.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ru.tatintec.gwttest.client.service.EmployeeReportService;
import ru.tatintec.gwttest.server.dao.EmployeeCatalog;
import ru.tatintec.gwttest.shared.Employee;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class EmployeeReportServiceImpl extends RemoteServiceServlet implements EmployeeReportService {

	/**
	 * this is a stub
	 *  //TODO represent it as DAO 
	 */
	private List<Employee> emplList = EmployeeCatalog.INSTANCE();

	@Override
	public Map<Integer, Integer> getNumberOfEmployeeByBirthYear() {

		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		
		for (Employee e : emplList) {
			Date birthday = e.getBirthday();
			Calendar calendar = GregorianCalendar.getInstance();
			calendar.setTime(birthday);
			Integer year = calendar.get(Calendar.YEAR);
			
			if (result.get(year) == null) {
				result.put(year, 1);
			} else {
				result.put(year, 1 + result.get(year));
			}
		}
		return result;
	}


	@Override
	public Map<String, Integer> getNumberOfEmployeeByEmploymentYear() {

		Map<String, Integer> result = new HashMap<String, Integer>();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		
		for (Employee e : emplList) {
			String key = dateFormat.format(e.getEmploymentDate());
			if (result.get(key) == null) {
				result.put(key, 1);
			} else {
				result.put(key, 1 + result.get(key));
			}
		}
		
		return result;
	}
}
